package com.ruoyi.project.poem.poemaudiorecom.service.impl;

import com.ruoyi.common.utils.ApiCallUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.poem.poem.domain.Poem;
import com.ruoyi.project.poem.poem.service.IPoemService;
import com.ruoyi.project.poem.poemaudiorecom.domain.PoemAudioRecom;
import com.ruoyi.project.poem.poemaudiorecom.mapper.PoemAudioRecomMapper;
import com.ruoyi.project.poem.poemaudiorecom.service.IPoemAudioRecomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 读诗审核Service业务层处理
 *
 * @author shixiangwen
 * @date 2022-07-04
 */
@Service
@DataSource(DataSourceType.POEMBBS)
public class PoemAudioRecomServiceImpl implements IPoemAudioRecomService {
    @Autowired
    private PoemAudioRecomMapper poemAudioRecomMapper;

    @Autowired
    IPoemService poemService;

    /**
     * 查询读诗审核
     *
     * @param articleId 读诗审核ID
     * @return 读诗审核
     */
    @Override
    public PoemAudioRecom selectPoemAudioRecomById(String articleId) {
        return poemAudioRecomMapper.selectPoemAudioRecomById(articleId);
    }

    /**
     * 查询读诗审核列表
     *
     * @param poemAudioRecom 读诗审核
     * @return 读诗审核
     */
    @Override
    public List<PoemAudioRecom> selectPoemAudioRecomList(PoemAudioRecom poemAudioRecom) {
        List<PoemAudioRecom> list = poemAudioRecomMapper.selectPoemAudioRecomList(poemAudioRecom);
        list.forEach(item -> {
            Poem poem = poemService.selectPoemById(item.getPoemId());
            if (poem != null) {
                item.setTitle(poem.getMingcheng());
            }
        });
        return list;
    }

    /**
     * 新增读诗审核
     *
     * @param poemAudioRecom 读诗审核
     * @return 结果
     */
    @Override
    public int insertPoemAudioRecom(PoemAudioRecom poemAudioRecom) {
        return poemAudioRecomMapper.insertPoemAudioRecom(poemAudioRecom);
    }

    /**
     * 修改读诗审核
     *
     * @param poemAudioRecom 读诗审核
     * @return 结果
     */
    @Override
    public int updatePoemAudioRecom(PoemAudioRecom poemAudioRecom) {
        return poemAudioRecomMapper.updatePoemAudioRecom(poemAudioRecom);
    }

    /**
     * 删除读诗审核对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePoemAudioRecomByIds(String ids) {
        return poemAudioRecomMapper.deletePoemAudioRecomByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除读诗审核信息
     *
     * @param articleId 读诗审核ID
     * @return 结果
     */
    @Override
    public int deletePoemAudioRecomById(String articleId) {
        return poemAudioRecomMapper.deletePoemAudioRecomById(articleId);
    }

    @Override
    public int approve(String articleId, Integer status) {
        PoemAudioRecom poemAudioRecom = poemAudioRecomMapper.selectPoemAudioRecomById(articleId);
        poemAudioRecom.setStatus(status);
        if (status == 1) {
            ApiCallUtil.setAudioRecom(articleId, 1);
        } else {
            ApiCallUtil.setAudioRecom(articleId, 0);
        }
        return poemAudioRecomMapper.updatePoemAudioRecom(poemAudioRecom);

    }
}
